const app = getApp()
import prizeApi from '../../api/prizeApi'
import randomNickname from '../../util/randomNickname'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    prizeId: '',
    content: '',
    imgList: [],
    fileList: [],
    date: '',

    deleteFiles: [],


  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.data.prizeId = options.prizeId

  },

  previewImage(e) {
    var url = e.currentTarget.dataset.url
    wx.previewImage({
      urls: this.data.imgList,
      current: url
    })
  },

  contentInput: function (e) {
    var val = e.detail.value;

    this.setData({
      content: val
    });
  },
  chooseImage: function () {
    wx.chooseMedia({
      count: 9 - this.data.imgList.length,
      mediaType: ['image'],

      success: (res) => {
        let list = res.tempFiles.map(obj => obj.tempFilePath)
        const tempFilePaths = this.data.imgList.concat(list)
        this.setData({
          imgList: tempFilePaths
        })
      }
    })
  },

  delImg(e) {

    var index = e.currentTarget.dataset.index
    this.data.imgList.splice(index, 1)
    var fileIds = this.data.fileList.splice(index, 1)
    if (fileIds[0]) {
      this.data.deleteFiles.push(fileIds[0])
    }
    this.setData({
      imgList: this.data.imgList
    })
  },


  getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
  },


  async submit() {



    if (!this.data.content) {
      wx.showToast({
        title: '没有可提交的内容',
        icon: 'none'
      })
      return
    }

    let count = parseInt(this.data.content + '')
    let list = []
    for (let i = 0; i < 300; i++) {
      list.push(i)
    }
    let ranList = []
    for(let i = 0; i<count; i++){
      let ran = this.getRandomInt(0, list.length)
      ranList.push(list[ran])
      list.splice(ran, 1)
    }
   

    wx.showLoading({
      mask: true
    })

    for(let ranIndex of ranList){
      var params = {
        prizeId: this.data.prizeId,
        // fileID: res[0].fileID,
        avatarUrl: `https://6161-aaa-0g95yu8l18c1a200-1331416746.tcb.qcloud.la/WeiXinHead/${ranIndex}.png`,
        nickname: randomNickname[ranIndex],
        openid: Math.random() + 'sdfd'
      }
  
      await prizeApi.addJoin(params)
    }
    


    // params.type = 'prize'
    // mediaCheck(params)



    wx.hideLoading()
    wx.navigateBack()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})